package com.csdj.newsmanger.controller;

import com.csdj.newsmanger.service.InformationService;
import com.csdj.newsmanger.vo.Informations;
import com.csdj.newsmanger.vo.Replies;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class InformationController {

    @Resource
    private InformationService informationService;

    @GetMapping("/index")
    public String index(Model model){
        System.out.println("hello");
        List<Informations> informationsList =
                informationService.queryAllInformation();
        model.addAttribute("informations",informationsList);

        return "index";
    }

    @GetMapping("/news/{id}")
    public String showNews(@PathVariable Long id,Model model){
        //修改 新闻的查看数 +1
        informationService.updateViewCount(id);
        //根据 编号 查询新闻的具体内容
        Informations informations = informationService.queryInformationById(id);
        model.addAttribute("info",informations);
        //根据 编号 查询 新闻的所有评论
        List<Replies> replies = informationService.queryAllRelies(id);
        model.addAttribute("replies",replies);
        return "news";
    }

    @PostMapping("/add")
    @ResponseBody
    public Map<String,Object> addRelies(@RequestBody Replies replies){
        int rows = informationService.addRelies(replies);
        Map<String,Object> map=new HashMap<>();
        if(rows>0){
            //修改新闻的 评论数  +1
            informationService.updateRelyCount(replies.getInfoid());
            map.put("code",200);
            map.put("msg","添加成功!");
        }else{
            map.put("code",300);
            map.put("msg","添加失败!");
        }
        return map;

    }


}
